package cn.lil.service;

import cn.lil.dao.BearingDao;
import cn.lil.dao.OrderDao;
import cn.lil.model.Bearing;
import cn.lil.model.Order;
import cn.lil.model.OrderItem;
import cn.lil.utils.DataSourceUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;

import java.sql.SQLException;
import java.util.List;

public class OrderService {
    private OrderDao orderDao;
    private BearingDao bearingDao = new BearingDao();

    public OrderService() {
        orderDao = new OrderDao();
    }

    public void insertOrder(Order order) throws SQLException {
        orderDao.insertOrder(order);
    }

    public void updateOrderAmountAndtotal(int orderId, int userId, int amount, float total) throws SQLException {
        orderDao.updateOrderAmountAndtotal(orderId,userId,amount,total);
    }

    public void insertOrderItem(OrderItem orderItem) throws SQLException {
        List<OrderItem> orderItemList = orderDao.selectAllitem(orderItem.getOrderId());
        boolean exists = false;
        for (OrderItem oi : orderItemList) {
            if (oi.getBearingId() == orderItem.getBearingId()) {
                exists = true;
                // 更新现有订单项的数量和价格
                Bearing b = bearingDao.selectBearingById(oi.getBearingId());
                orderDao.updateOrderItemAmountAndPrice(oi.getOrderId(), oi.getBearingId(),
                        oi.getAmount() + orderItem.getAmount(),
                        b.getPrice()*(oi.getAmount()+orderItem.getAmount()));
                break; // 找到匹配项后跳出循环
            }
        }
        if (!exists) {
            // 如果没有找到匹配项，则插入新的订单项
            orderDao.insertOrderItem(orderItem);
        }
    }

    public List<Order> selectAll(int userId) throws SQLException {
        List<Order> list = null;
        list = orderDao.selectAll(userId);
        if(list.isEmpty()){
            Order order = new Order(0,0.00f,0,0,0,userId,null);
            orderDao.insertOrder(order);
            list = orderDao.selectAll(userId);
        }
        for (Order o : list) {
            List<OrderItem> l = orderDao.selectAllitem(o.getId());
            o.setItemList(l);
        }
        return list;
    }

    public List<OrderItem> selectAllitem(int orderId) throws SQLException {
        return orderDao.selectAllitem(orderId);
    }

    public int getOrderCount(int status) throws SQLException {
        return orderDao.getOrderCount(status);
    }

    public List<Order> selectOrderList(int status,int uid) throws SQLException {
        return orderDao.selectOrderList(status,uid);
    }

    public void updateStatus(int id) throws SQLException {
        orderDao.updateStatus(id);
    }

    public void deleteOrder(int id) throws SQLException {
        orderDao.deleteOrder(id);
    }

    public void deleteOrderItem(int id) throws SQLException {
        orderDao.deleteOrderItem(id);
    }

    public void addOrder(Order o) throws SQLException {
        orderDao.insertOrder(o);
        int id = orderDao.getLastInsertId();
        o.setId(id);
        for (OrderItem item : o.getItemMap().values()) {
            orderDao.insertOrderItem(item);
        }
    }

    public void deleted(int id) throws SQLException {
        orderDao.deleteOrderItem(id);
        orderDao.deleteOrder(id);
    }
}
